<?php
// helloworld.php demonstrates a very basic xajax implementation
// using xajax version 0.1 beta4
// http://xajax.sourceforge.net

//echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";

require ('xajax.inc.php');
require ('config.php');

// get client ip address
$ip = $_SERVER['REMOTE_ADDR'];	

function readfromfile($restaurant)
{
	$today = date("Ymd");
	$filename = $restaurant . "_" . $today . ".dat";
	if (file_exists("data/$filename"))
		{
			$config = parse_ini_file("data/$filename");
		}
	else
		{
			$fh = fopen("data/$filename","x+") or die ("cant create new file!!");
			fclose($fh) or die ("file close error!!");
			$config = parse_ini_file("data/$filename");
		}
	
	//$config = parse_ini_file('dinnerdata.dat');
	
	return $config;
}

function savetofile($restaurant,$dishes)
{
	$today = date("Ymd");
	$filename = $restaurant . "_" . $today . ".dat";

	foreach ($dishes as $key=>$value) {
                $text .= "$key=$value\n";
	}	
	// open the file for reading and writing
	$fh = fopen("data/$filename",'r+') or die("cant open file to write back!!");
	// write back to file
	if (-1 == fwrite($fh,$text)) { die("file write error!!"); }
	// adjust the file's length to just what's been written
	ftruncate($fh,ftell($fh)) or die("file truncate error!!");
	// close the file
	fclose($fh) or die("file close error!!");
}

function loaddishes($restaurant)
{
	if ($restaurant == 'default')
	{
		global $menubook;
		$resaurants = array_keys($menubook);
		$restaurant = $resaurants[0];
	}
	
	global $iptoname;
	$dishes = readfromfile($restaurant);
	
	foreach ($dishes as $key=>$value) {
		$name = $iptoname[$key]?$iptoname[$key]:$key;
		$text .= "<p>$value <span class='author'>$name</span></p><img src='images/comment_bg.gif' alt='comment bottom'/>";
	}
	
	$objResponse = new xajaxResponse();
	$objResponse->addAssign("comments","innerHTML",$text);
	return $objResponse;
}

function loadmenu($restaurant)
{
	$text = assemble_menu($restaurant);
	$objResponse = new xajaxResponse();
	$objResponse->addAssign("text","innerHTML",$text);	
	return $objResponse;
}

function loadrestaurants()
{
	global $menubook;
	
	foreach ($menubook as $restaurant => $types_array )
	{
		$string .= "<li><a href=\"#\"  onclick=\"xajax_loadmenu('$restaurant');xajax_loaddishes('$restaurant');\">$restaurant</a></li>\n";
	}
	$objResponse = new xajaxResponse();
	$objResponse->addAssign("menuitem","innerHTML",$string);
	return $objResponse;	
}

function assemble_menu($restaurant)
{
	global $ip;
	global $menubook;
		
	if ($restaurant == 'default')
	{
		$resaurants = array_keys($menubook);
		$restaurant = $resaurants[0];
	}
	foreach ( $menubook[$restaurant] as $type=>$dish_array )
	{
		$i = 0;
		$string .= "\n<h1><span>$type</span></h1>\n";
		$string .= "<div class=\"sideMenu\">\n<table width=100%>\n";
		
		foreach ($dish_array as $dish=>$detail_array)
		{
			if ($i%2 == 0) $string .= "<tr>";
			$string .= "<td><a href=\"#\" onclick=\"xajax_post('$restaurant','$dish','$ip');\"><span>$dish|";
			foreach ($detail_array as $detail )
			{
				$string .= " $detail";
			}
			$string .= "</span></a></td>";
			if ($i%2 == 1) $string .= "</tr>";
			$i++;
		}
		$string .= "</table></div>";
	}
	
	return $string;
}

function post($restaurant,$dish,$user)
{
    $dishes = readfromfile($restaurant);
	$dishes["$user"] = $dish;
	savetofile($restaurant,$dishes);
    //$objResponse = new xajaxResponse();
    //$objResponse->addAssign("div2","innerHTML","saved:$dish by $user");
    //return $objResponse;
	return loaddishes($restaurant);
}

// Instantiate the xajax object.  No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax(); 

//$xajax->debugOn(); // Uncomment this line to turn debugging on

// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
$xajax->registerFunction("loaddishes");
$xajax->registerFunction("setColor");
$xajax->registerFunction("post");
$xajax->registerFunction("loadmenu");
$xajax->registerFunction("loadrestaurants");

// Process any requests.  Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();

?>






<html xmlns="http://www.w3.org/1999/xhtml">

<!--
 ____________________________________________________________
|                                                            |
|    DESIGN + Pat Heard { http://fullahead.org }             |
|      DATE + 2005.11.30                                     |
| COPYRIGHT + Free use if this notice is left in place       |
|____________________________________________________________|

-->

<head>


  <title>{ Greenery } Open Web Design <?=date("Ymd");?></title>

  <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
  <meta name="author" content="fullahead.org" />
  <meta name="keywords" content="Open Web Design, OWD, Free Web Template, Greenery, Fullahead" />
  <meta name="description" content="A free web template designed by Fullahead.org and hosted on OpenWebDesign.org" />
  <meta name="robots" content="index, follow, noarchive" />
  <meta name="googlebot" content="noarchive" />

  <link rel="stylesheet" type="text/css" href="css/screen.css" media="screen, print" />

  <?php $xajax->printJavascript('.'); // output the xajax javascript. This must be called between the head tags ?>
  
</head>


<body onload="xajax_loadrestaurants();xajax_loadmenu('default');xajax_loaddishes('default');">

  <input type="hidden" id="ip" name="ip" value="<?=$ip?>"/>

  <div id="header">

    <div id="menu">

      <ul id="menuitem">
       	<!--restaurants lies here-->
      </ul>

      <div id="title">
        <h1>Greenery</h1>
      </div>

    </div>

  </div>

  <div id="content">

    <div id="text">
    	<!--menus lies here-->
    </div>

    <div id="image"><img src="images/plants.jpg" alt="plants"/></div>

    <div id="sideBar">

      <h1><span>Comments</span></h1>

      <div id="comments">
      		<!--dishes lies here-->
      		<p>今天还没人点过 <span class="author"> 服务器 </span></p>
      </div>

      <h1><span>Your Turn</span></h1>


    </div>

    <div id="footer">
      <a href="http://fullahead.org" title="designed by fullahead.org" class="fullAhead"></a>
      <p>Valid <a href="http://validator.w3.org/check?uri=referer" title="validate XHTML">XHTML</a> &amp; <a href="http://jigsaw.w3.org/css-validator" title="validate CSS">CSS</a></p>
    </div>

  </div>

</body>

</html>
